package com.qnbc.mystery.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.qnbc.mystery.core.controller.BaseController;
import com.qnbc.mystery.core.domain.AjaxResult;
import com.qnbc.mystery.core.page.TableDataInfo;
import com.qnbc.mystery.core.utils.poi.ExcelUtil;
import com.qnbc.mystery.pojo.domain.User;
import com.qnbc.mystery.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qnbc.mystery.pojo.domain.Address;
import com.qnbc.mystery.service.IAddressService;

/**
 * 地址
 *
 * @author guzj
 * @date 2023-12-27
 */
@Api(tags = "地址管理")
@RestController
@RequestMapping("/address")
public class AddressController extends BaseController {
    @Autowired
    private IAddressService addressService;

    /**
     * 查询地址列表
     */
    @ApiOperation(value = "地址列表")
    @ResponseBody
    @GetMapping("/list")
    public TableDataInfo list(Address address) {
        startPage();
//        PageHelper.startPage(address.getPageNum(), address.getPageSize());
        List<Address> list = addressService.selectAddressList(address);
//        PageInfo pageInfo = new PageInfo(list);
        return getDataTable(list);
    }

    /**
     * 导出地址列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Address address) {
        List<Address> list = addressService.selectAddressList(address);
        ExcelUtil<Address> util = new ExcelUtil<Address>(Address.class);
        util.exportExcel(response, list, "地址数据");
    }

    /**
     * 获取地址详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(addressService.selectAddressById(id));
    }


    /**
     * 设置默认地址
     */
    @ApiOperation(value = "设置默认地址")
    @GetMapping(value = "/setDefault/{id}")
    public AjaxResult setDefault(@PathVariable("id") Long id) {
        return AjaxResult.success(addressService.setDefault(id));
    }

    /**
     * 新增地址
     */
    @ApiOperation(value = "新增地址")
    @ResponseBody
    @PostMapping
    public AjaxResult add(@RequestBody Address address) {
        User currentUser = TokenUtils.getCurrentUser();
        if (null == currentUser) {
            address.setCreatedBy(address.getCreatedBy());
        } else {
            address.setCreatedBy(String.valueOf(currentUser.getId()));
        }
        address.setCreatedTime(new Date());
        return toAjax(addressService.insertAddress(address));
    }


    /**
     * 修改地址
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Address address) {
        return toAjax(addressService.updateAddress(address));
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(addressService.deleteAddressByIds(ids));
    }
}
